
var id="";
readercourse();//课程列表
courselist();//全部数据

function readercourse(){
  $.ajax({
    url:"/api/coursetypes",
    method:"get",
    success:function(data){
        coursetypes(data);
    }
  })

  function coursetypes(data) {
    var $fragment = $(document.createDocumentFragment())
    $.each(data.data, function(index, item) {
      if (index === 0) {
        var $courseTypeAll = $('<li class="uk-active"><a>'+i18next.t('home.all')+'</a></li>').appendTo($fragment)
        $courseTypeAll.on('click', function() {
          $(this).addClass('uk-active').siblings().removeClass('uk-active')
          courselist();
        })
      }
      var $courseType = $('<li><a>' + item.coursetypename + '</a></li>').appendTo($fragment)
      $courseType.on('click', function() {
        $(this).addClass('uk-active').siblings().removeClass('uk-active')
        courseItermlist(item.coursetypeuid);
      })
    })
    $fragment.appendTo($('#J_coursetypes'))
  }
}

//每个科目的数据
function courseItermlist(coursetypeuid){
  $.ajax({
    url:"/api/teachers/type/" + coursetypeuid,
    method:"get",
    data:{
      status:1
    },
    success:function(data){
        teachers(data)
    }
  })
}
//全部的数据
function courselist(){
  $.ajax({
    url:"/api/teachers?status=1",
    method:"get",
    success:function(data){
        teachers(data);
    }
  })

}

function loadTeacher(data) {
  if(!data.length) {
    $('#j-teachers').hide()
    $('#j-teacher-empty').show().text(i18next.t('home.nodatas'))//暂无数据
    return;
  }
  var htmlStr = ''
  $.each(data, function (i, item) {
    var defaultAvatar = ""

    htmlStr += '<a href="teacher.html?id='+ item.teacheruid +'" class="teacher">\
      <div class="uk-panel uk-panel-box uk-text-center">\
        <img class="lazy uk-border-circle" width="120" height="120" data-original="'+ item.photo +'" src="'+ defaultAvatar +'" alt="">\
        <h3 class="uk-margin-bottom-remove">'+ item.lastname +'</h3>\
        <p class="uk-text-large uk-margin-small-top uk-text-primary">'+ (item.title||i18next.t('home.notitle')) +'</p>\
        <p class="uk-text-left cc-text-truncate-multi">'+ item.introduce +'</p>\
      </div>\
    </a>'
  })

  $('#j-teacher-empty').hide()
  $('#j-teachers').show().html(htmlStr)

  imgLazyload()
}

//渲染批量数据
function teachers(data){
  loadTeacher(data.data)
}
